<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>猜数字游戏</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
    }

    h1 {
      margin-bottom: 20px;
    }

    input {
      padding: 5px;
      margin-right: 10px;
    }

    button {
      padding: 5px 10px;
    }
  </style>
</head>

<body>
  <h1>猜数字游戏 (1-100)</h1>
  <input type="number" id="guessInput" min="1" max="100" placeholder="请输入你的猜测">
  <button onclick="checkGuess()">提交</button>
  <div id="result"></div>

  <script>
    // 生成一个1 - 100之间的随机数
    const targetNumber = Math.floor(Math.random() * 100) + 1;

    function checkGuess() {
      const userGuess = parseInt(document.getElementById('guessInput').value);
      const resultDiv = document.getElementById('result');

      if (isNaN(userGuess)) {
        resultDiv.innerHTML = "请输入有效的数字！";
        return;
      }

      if (userGuess === targetNumber) {
        resultDiv.innerHTML = "恭喜你，猜对了！";
        document.getElementById('guessInput').disabled = true;
        document.querySelector('button').disabled = true;
      } else if (userGuess > targetNumber) {
        resultDiv.innerHTML = "太大了！再试一次。";
      } else {
        resultDiv.innerHTML = "太小了！再试一次。";
      }
    }
  </script>
</body>

</html>